package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 开心人大药房
 * @author tngou@tngou.net
 *
 */
public class COM_360kxr  extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_360kxr.class);
	
	private String url="http://www.360kxr.com/product/{}.html";
	


	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.getElementById("pro_details")==null) return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("/");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements procrumbs = doc.getElementsByClass("procrumbs");
		if(procrumbs.first()!=null)
		{
			Elements as = procrumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 2; i < as.size()-1; i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		procrumbs = doc.getElementsByClass("position");
		if(procrumbs.first()!=null)
		{
			Elements as = procrumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size()-1; i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		
		//图片
		Elements items = doc.getElementsByClass("items");
		if(items.first()!=null)
		{
			Elements imgs = items.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		
		Element bot = doc.getElementById("sp-bot");
		if(bot==null)bot=doc.getElementById("con_three_4");
		if(bot!=null)
		{
			Elements imgs = bot.getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//价格
		Element price = doc.getElementById("pro_mall_price");
		if(price!=null)
		{			
			String value = price.text();	
			json.put("price", getPrice(value));
			
		}
		//说明书
		Elements instruction = doc.getElementsByClass("neirong");
		if(instruction.first()!=null)
		{
			
			JSONObject instructions = instructions_tr_td_td(instruction.first());
			Element pro_details = doc.getElementById("pro_details");
			if(pro_details!=null)
			instructions = instructions(pro_details,instructions);
			json.put("instructions",instructions );
			
		}
		instruction = doc.getElementsByClass("goods-info");
		if(instruction.first()!=null)
		{
			
			JSONObject instructions = instructions_tr_td_td(instruction.first());
			Element pro_details = doc.getElementById("pro_details");
			if(pro_details!=null)
			instructions = instructions(pro_details,instructions);
			json.put("instructions",instructions );
			
		}
		return json;
	}
	private JSONObject instructions(Element element, JSONObject json) {
		Elements lis = element.getElementsByTag("li");
		for (Element li : lis) {
			String[] es = StringUtil.split(li.text(), "：");
			if(es.length==2&&!"商品编号".equals(es[0])) {
				json.put(StringUtil.cleanKey(es[0]), StringUtil.RemoveEmpty(es[1]));
			}
		}
		
		return name_number(json);
	}
	
	
}
